<?php

class Players	 extends CI_Controller {

	/*function edit() {

	 $this -> load -> helper( array('form', 'url'));

	 $this -> load -> library('form_validation');

	 //$this -> form_validation -> set_rules('username', 'Username', 'trim|required|min_length[5]|max_length[12]|xss_clean');
	 //$this -> form_validation -> set_rules('password', 'Password', 'trim|required|matches[passconf]|md5');
	 //$this -> form_validation -> set_rules('passconf', 'Password Confirmation', 'trim|required');
	 //$this -> form_validation -> set_rules('email', 'Email', 'trim|required|valid_email');

	 $this -> load -> view('players_edit_view');
	 /*
	 if($this -> form_validation -> run() == FALSE) {
	 $this -> load -> view('myform');
	 } else {
	 $this -> load -> view('formsuccess');
	 }
	 }*/

	function playerinfo($id=FALSE) {

		if($id) {

			$sql = "SELECT player_id, player_name, city, birth_date FROM players where player_id=" . $id . ";";

			$query = $this -> db -> query($sql);

			if($query -> num_rows() > 0) {
				$row = $query -> row(0);

				$data['player_name'] = $row -> player_name;
				$data['city'] = $row -> city;
				$data['player_id'] = $row -> player_id;
				$data['birth_date'] = $row -> birth_date;

				//$this -> load -> view('player_info_view',$data);
				//echo $row -> player_name;
				//echo $row -> city;
				// call attributes
				//echo $row -> reverse_name();
				// or methods defined on the 'User' class

				$this -> db -> select('teams.team_id,teams.team_name, teams.category, team_player_rel.player_number, team_player_rel.captain_flag, category.category_desc, category.category_sport');
				$this -> db -> from('teams');
				$this -> db -> join('team_player_rel', 'team_player_rel.team_id = teams.team_id');
				$this -> db -> join('category', 'category.category_id = teams.category');
				$this -> db -> where('player_id', $id);
				$query = $this -> db -> get();

				$teams = array();
				foreach($query->result() as $row) {
					array_push($teams, $row);
					//$players[$row -> player_id] = $row -> player_name;
				}
				$data['teams'] = $teams;

				$this -> load -> view('player_info_view', $data);
				//print_r($data);

			} else {

			}

		}

	}

	function save() {

		//print_r($_POST);
		echo $_POST['name'] . "\n";
		echo $_POST['city'] . "\n";
		echo $_POST['operation'] . "\n";
		echo $_POST['birth'] . "\n";

		//$timestamp = strtotime($_POST['birth']);

		$pieces = explode("/", $_POST['birth']);
		$d = $pieces[0];
		$m = $pieces[1];
		$y = $pieces[2];

		//echo $timestamp;
		
		$datebirth = $y."-".$m."-".$d;
		
		echo $datebirth;

		

		// Decode base64 data to insert it
		// Remember that codeigniter already add slashes to escape chars
		$img_data = base64_decode($_REQUEST["bindata"]);
		//$imgData = addslashes($img_data);

		$data = array('player_name' => $_POST['name'], 'city' => $_POST['city'], 'photo_format' => '.jpg', 'birth_date' => $datebirth , 'photo' => $img_data);

		$this -> db -> insert('players', $data);

	}

	function create() {
		//Load view with the flex form (Webcam snapshot)
		$this -> load -> view('player_new_view');
	}

	function image($id=FALSE) {

		if($id && is_numeric($id)) {

			$sql = "SELECT photo FROM players WHERE player_id=" . $id . "";

			$query = $this -> db -> query($sql);
			if($query -> num_rows() > 0) {
				$row = $query -> row(0);
				header("Content-type: image/jpeg");
				echo $row -> photo;

			} else {
				echo "id not found";
			}
		} else {
			echo 'Please use a real id number';
		}

	}

}
?>